From bdc6395d648708d4948dfb654af9947033c953ac Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Mon, 16 Jul 2012 17:29:01 +0200 Subject: [PATCH] New gtk-launch command This program launches an application specified by its desktop name optinally taking list of URIs which are passed as arguments. Uses GdkAppLaunchContext to get proper startup notification and display handling for graphical apps. https://bugzilla.gnome.org/show_bug.cgi?id=679342 --- docs/reference/gtk/Makefile.am | 4 +- docs/reference/gtk/gtk-docs.sgml | 1 + docs/reference/gtk/gtk-launch.xml | 72 +++++++++++++++ gtk/Makefile.am | 6 +- gtk/gtk-launch.c | 149 ++++++++++++++++++++++++++++++ 5 files changed, 230 insertions(+), 2 deletions(-) create mode 100644 docs/reference/gtk/gtk-launch.xml create mode 100644 gtk/gtk-launch.c diff --git a/docs/reference/gtk/Makefile.am b/docs/reference/gtk/Makefile.am index db1f0123a8..8ef4044bd4 100644 --- a/docs/reference/gtk/Makefile.am +++ b/docs/reference/gtk/Makefile.am @@ -141,6 +141,7 @@ content_files = \ x11.sgml \ gtk-query-immodules-3.0.xml \ gtk-update-icon-cache.xml \ + gtk-launch.xml \ visual_index.xml \ getting_started.xml \ overview.xml @@ -400,7 +401,8 @@ EXTRA_DIST += version.xml.in gtk3.types.in man_MANS = \ gtk-query-immodules-3.0.1 \ - gtk-update-icon-cache.1 + gtk-update-icon-cache.1 \ + gtk-launch.1 if ENABLE_MAN diff --git a/docs/reference/gtk/gtk-docs.sgml b/docs/reference/gtk/gtk-docs.sgml index 6eca4d0dce..098d5d73d6 100644 --- a/docs/reference/gtk/gtk-docs.sgml +++ b/docs/reference/gtk/gtk-docs.sgml @@ -377,6 +377,7 @@ GTK+ Tools + diff --git a/docs/reference/gtk/gtk-launch.xml b/docs/reference/gtk/gtk-launch.xml new file mode 100644 index 0000000000..17fc540cfd --- /dev/null +++ b/docs/reference/gtk/gtk-launch.xml @@ -0,0 +1,72 @@ + + + + + + gtk-launch + gtk+ + + + Developer + Tomáš + Bžatek + tbzatek@redhat.com + + + + + + gtk-launch + 1 + User Commands + + + + gtk-launch + Launch an application + + + + +gtk-launch +APPLICATION +URI + + + +Description + +gtk-launch launches an application using the given name. +The application is started with proper startup notification on a default +display, unless specified otherwise. + + +gtk-launch takes at least one argument, the name of +the application to launch. The name should match application desktop file name, +as residing in /usr/share/application, with or without the '.desktop' suffix. + + +If called with more than one argument, the rest of them besides the application name +are considered URI locations and are passed as arguments to the launched application. + + + +Options + The following options are understood: + + + , + Prints a short help text and exits. + + + + +Bugs + +None known yet. + + + + diff --git a/gtk/Makefile.am b/gtk/Makefile.am index 41c61b86d1..397fa4fa25 100644 --- a/gtk/Makefile.am +++ b/gtk/Makefile.am @@ -1286,7 +1286,8 @@ endif # Installed tools # bin_PROGRAMS = \ - gtk-query-immodules-3.0 + gtk-query-immodules-3.0 \ + gtk-launch if BUILD_ICON_CACHE bin_PROGRAMS += gtk-update-icon-cache @@ -1333,6 +1334,9 @@ gtk_update_icon_cache_LDADD = $(GDK_PIXBUF_LIBS) gtk_update_icon_cache_SOURCES = updateiconcache.c endif +gtk_launch_LDADD = $(LDADDS) +gtk_launch_SOURCES = gtk-launch.c + .PHONY: files test test-debug files: diff --git a/gtk/gtk-launch.c b/gtk/gtk-launch.c new file mode 100644 index 0000000000..6d5be1ccad --- /dev/null +++ b/gtk/gtk-launch.c @@ -0,0 +1,149 @@ +/* GTK - The GIMP Toolkit + * + * Copyright (C) 2012 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library. If not, see . + * + * Author: Tomas Bzatek + */ + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +static gchar **args = NULL; + +static GOptionEntry entries[] = { + { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args, NULL, NULL }, + { NULL} +}; + +int +main (int argc, char *argv[]) +{ + GError *error = NULL; + GOptionContext *context = NULL; + gchar *summary; + gchar *app_name; + gchar *desktop_file_name; + GDesktopAppInfo *info; + GAppLaunchContext *launch_context; + GList *l; + GFile *f; + + setlocale (LC_ALL, ""); + +#ifdef ENABLE_NLS + bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR); + textdomain (GETTEXT_PACKAGE); +#ifdef HAVE_BIND_TEXTDOMAIN_CODESET + bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); +#endif +#endif + + g_type_init (); + + /* Translators: this message will appear immediately after the */ + /* usage string - Usage: COMMAND [OPTION]... */ + context = + g_option_context_new (_("APPLICATION [URI...] - launch an APPLICATION with URI.")); + + /* Translators: this message will appear after the usage string */ + /* and before the list of options. */ + summary = _("Launch specified application by its desktop file info\n" + "optionally passing list of URIs as arguments."); + g_option_context_set_summary (context, summary); + g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); + g_option_context_add_group (context, gtk_get_option_group (FALSE)); + + g_option_context_parse (context, &argc, &argv, &error); + + g_option_context_free (context); + + if (error != NULL) + { + g_printerr (_("Error parsing commandline options: %s\n"), error->message); + g_printerr ("\n"); + g_printerr (_("Try \"%s --help\" for more information."), + g_get_prgname ()); + g_printerr ("\n"); + g_error_free(error); + return 1; + } + + if (!args) + { + /* Translators: the %s is the program name. This error message */ + /* means the user is calling gtk-launch without any argument. */ + g_printerr (_("%s: missing application name"), g_get_prgname ()); + g_printerr ("\n"); + g_printerr (_("Try \"%s --help\" for more information."), + g_get_prgname ()); + g_printerr ("\n"); + return 1; + } + + + gtk_init (&argc, &argv); + + app_name = *args; + if (g_str_has_suffix (app_name, ".desktop")) + desktop_file_name = g_strdup (app_name); + else + desktop_file_name = g_strconcat (app_name, ".desktop", NULL); + info = g_desktop_app_info_new (desktop_file_name); + g_free (desktop_file_name); + args++; + + if (!info) + { + /* Translators: the first %s is the program name, the second one */ + /* is the application name. */ + g_printerr (_("%s: no such application %s"), + g_get_prgname (), app_name); + g_printerr ("\n"); + return 2; + } + + l = NULL; + for (; *args; args++) + { + f = g_file_new_for_commandline_arg (*args); + l = g_list_append (l, f); + } + + launch_context = (GAppLaunchContext*) gdk_display_get_app_launch_context (gdk_display_get_default ()); + if (!g_app_info_launch (G_APP_INFO (info), l, launch_context, &error)) + { + /* Translators: the first %s is the program name, the second one */ + /* is the error message. */ + g_printerr (_("%s: error launching application: %s\n"), + g_get_prgname (), error->message); + return 3; + } + g_object_unref (info); + g_object_unref (launch_context); + g_list_free_full (l, g_object_unref); + + return 0; +} -- 2.30.2